"use client"

import { Suspense } from "react"
import {
  BrowserRouter as Router,
  Routes,
  Route,
  Navigate,
} from "react-router-dom"
import { Spin } from "antd"

// 直接导入需要的组件
import Login from "../pages/Login"
import Dashboard from "../pages/Dashboard"
import CustomerManagement from "../pages/CustomerManagement"
import EventManagement from "../pages/EventManagement"
import StaffManagement from "../pages/StaffManagement"
import Settings from "../pages/Settings"
import MainLayout from "../components/Layout/MainLayout"

const RouterComponent = ({ isLoggedIn, handleLogin, handleLogout }) => {
  return (
    <Router>
      <Suspense
        fallback={
          <Spin
            tip="加载中..."
            style={{ position: "fixed", top: "50%", left: "50%" }}
          />
        }
      >
        <Routes>
          {/* 登录路由 */}
          <Route
            path="/login"
            element={
              isLoggedIn ? (
                <Navigate to="/dashboard" replace />
              ) : (
                <Login onLogin={handleLogin} />
              )
            }
          />

          {/* 主布局路由 */}
          <Route
            path="/"
            element={
              isLoggedIn ? (
                <MainLayout onLogout={handleLogout} />
              ) : (
                <Navigate to="/login" replace />
              )
            }
          >
            <Route index element={<Dashboard />} />
            <Route path="dashboard" element={<Dashboard />} />
            <Route path="customers" element={<CustomerManagement />} />
            <Route path="events" element={<EventManagement />} />
            <Route path="staff" element={<StaffManagement />} />
            <Route path="settings" element={<Settings />} />
          </Route>

          {/* 捕获所有其他路由 */}
          <Route
            path="*"
            element={
              <Navigate to={isLoggedIn ? "/dashboard" : "/login"} replace />
            }
          />
        </Routes>
      </Suspense>
    </Router>
  )
}

export default RouterComponent
